/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author mwh
 */
public class DistortionTableCache implements DistortionTable {

    DistortionTable table;

    Map<DistortionKey, Double> cache;


    public DistortionTableCache(DistortionTable table, int size) {
        this.table = table;

        final long maxsize = Math.round(size * 0.80);

        cache = new LinkedHashMap<DistortionKey, Double>(size, 0.75F, true) {

            @Override
            protected boolean removeEldestEntry(Map.Entry<DistortionKey, Double> eldest) {

                return size() > maxsize;
            }
        };
    }


    public double get(DistortionKey k) {
        Double d = cache.get(k);
        if (d == null) {
            d = table.get(k);
            cache.put(k, d);
        }
        return d;


    }


    public double getLog10(DistortionKey k) {
        return Math.log10(get(k));
    }
}
